package com.muyu.danmo.service.impl;

import com.muyu.common.core.utils.StringUtils;
import com.muyu.danmo.domain.DanmoTrainingInformation;
import com.muyu.danmo.domain.DanmoTrainingInformationPicture;
import com.muyu.danmo.mapper.DanmoTrainingInformationPictureMapper;
import com.muyu.danmo.service.IDanmoTrainingInformationPictureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 培训班相关照片Service业务层处理
 *
 * @author yangle
 * @date 2024-10-10
 */
@Service
public class DanmoTrainingInformationPictureServiceImpl implements IDanmoTrainingInformationPictureService
{
    @Autowired
    private DanmoTrainingInformationPictureMapper danmoTrainingInformationPictureMapper;

    /**
     * 查询培训班相关照片
     *
     * @param id 培训班相关照片主键
     * @return 培训班相关照片
     */
    @Override
    public DanmoTrainingInformationPicture selectDanmoTrainingInformationPictureById(Long id)
    {
        return danmoTrainingInformationPictureMapper.selectDanmoTrainingInformationPictureById(id);
    }

    /**
     * 查询培训班相关照片列表
     *
     * @param danmoTrainingInformationPicture 培训班相关照片
     * @return 培训班相关照片
     */
    @Override
    public List<DanmoTrainingInformationPicture> selectDanmoTrainingInformationPictureList(DanmoTrainingInformationPicture danmoTrainingInformationPicture)
    {
        return danmoTrainingInformationPictureMapper.selectDanmoTrainingInformationPictureList(danmoTrainingInformationPicture);
    }

    /**
     * 新增培训班相关照片
     *
     * @param danmoTrainingInformationPicture 培训班相关照片
     * @return 结果
     */
    @Transactional
    @Override
    public int insertDanmoTrainingInformationPicture(DanmoTrainingInformationPicture danmoTrainingInformationPicture)
    {
        int rows = danmoTrainingInformationPictureMapper.insertDanmoTrainingInformationPicture(danmoTrainingInformationPicture);
       insertDanmoTrainingInformation(danmoTrainingInformationPicture);
        return rows;
    }

    /**
     * 修改培训班相关照片
     *
     * @param danmoTrainingInformationPicture 培训班相关照片
     * @return 结果
     */
    @Transactional
    @Override
    public int updateDanmoTrainingInformationPicture(DanmoTrainingInformationPicture danmoTrainingInformationPicture)
    {
        danmoTrainingInformationPictureMapper.deleteDanmoTrainingInformationById(danmoTrainingInformationPicture.getId());
        insertDanmoTrainingInformation(danmoTrainingInformationPicture);
        return danmoTrainingInformationPictureMapper.updateDanmoTrainingInformationPicture(danmoTrainingInformationPicture);
    }

    /**
     * 批量删除培训班相关照片
     *
     * @param ids 需要删除的培训班相关照片主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDanmoTrainingInformationPictureByIds(Long[] ids)
    {
        danmoTrainingInformationPictureMapper.deleteDanmoTrainingInformationByIds(ids);
        return danmoTrainingInformationPictureMapper.deleteDanmoTrainingInformationPictureByIds(ids);
    }

    /**
     * 删除培训班相关照片信息
     *
     * @param id 培训班相关照片主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDanmoTrainingInformationPictureById(Long id)
    {
        danmoTrainingInformationPictureMapper.deleteDanmoTrainingInformationById(id);
        return danmoTrainingInformationPictureMapper.deleteDanmoTrainingInformationPictureById(id);
    }

    /**
     * 新增培训班详情信息
     *
     * @param danmoTrainingInformationPicture 培训班相关照片对象
     */
    public void insertDanmoTrainingInformation(DanmoTrainingInformationPicture danmoTrainingInformationPicture)
    {
        List<DanmoTrainingInformation> danmoTrainingInformationList = danmoTrainingInformationPicture.getDanmoTrainingInformationList();
        Long id = danmoTrainingInformationPicture.getId();
        if (StringUtils.isNotNull(danmoTrainingInformationList))
        {
            List<DanmoTrainingInformation> list = new ArrayList<DanmoTrainingInformation>();
            for (DanmoTrainingInformation danmoTrainingInformation : danmoTrainingInformationList)
            {
                danmoTrainingInformation.setId(id);
                list.add(danmoTrainingInformation);
            }
            if (list.size() > 0)
            {
                danmoTrainingInformationPictureMapper.batchDanmoTrainingInformation(list);
            }
        }
    }
}
